package cn.cdeden.knowledge.controller.collection;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.dto.FilesCollectionDto;
import cn.cdeden.knowledge.domain.dto.FilesCollectionQueryParam;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.service.FilesCollectionService;
import cn.cdeden.system.api.model.LoginUser;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *我的收藏管理
 */
@Controller
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/filesCollection")
public class FilesCollectionController {

    private final FilesCollectionService filesCollectionService;

//    @Log("加入收藏")
//    @ApiOperation("加入收藏")

    /**
     * 加入收藏
     * @param resources
     * @return
     * @throws BadRequestException
     */
    @PostMapping(value = "/addCollection")
    @ResponseBody
    public R addCollection(@RequestBody FilesCollectionDto resources) throws BadRequestException {
        LoginUser loginUser = LoginHelper.getLoginUser();
        resources.setUserId(String.valueOf(loginUser.getUserId()));
        return R.ok(filesCollectionService.insert(resources));
//        return BaseRespResult.successResult(filesCollectionService.insert(resources));
    }

    /**
     * 取消收藏
     * @param resources
     * @return
     */
//    @Log("取消收藏")
//    @ApiOperation("取消收藏" )
    @PostMapping(value = "/cancelCollection")
    @ResponseBody
    public R cancelCollection(@RequestBody FilesCollectionDto resources){
        LoginUser loginUser = LoginHelper.getLoginUser();
        resources.setUserId(String.valueOf(loginUser.getUserId()));
        return R.ok(filesCollectionService.removeByMap(resources));
//        return BaseRespResult.successResult(filesCollectionService.removeByMap(resources));
    }


    /**
     * 查询我的收藏管理
     * @param query
     * @param pageable
     * @return
     */
//    @Log("查询我的收藏管理")
//    @ApiOperation("查询我的收藏管理")
    @GetMapping(value = "/query")
    @ResponseBody
    public R query(FilesCollectionQueryParam query, Pageable pageable){
        LoginUser loginUser = LoginHelper.getLoginUser();
        query.setUserId(loginUser.getUserId());
        TableDataInfo<FilesTree> filesTreeTableDataInfo = filesCollectionService.queryAll(query, pageable);
        return R.ok(filesTreeTableDataInfo);
//        PageInfo<FilesTree> info = filesCollectionService.queryAll(query, pageable);
//        info.setSize(pageable.getPageSize());
//        info.setPage(pageable.getPageNumber());
//        return BaseRespResult.successResultPage(info);
    }
}
